<template>
  <el-card>
    <el-row class="flex-center">
      <Logo></Logo>
    </el-row>
    <el-row class="flex-center">
      <h2>重置密码</h2>
    </el-row>
    <el-form ref="resetForm" :model="formData" :rules="rules" size="medium" label-width="100px">
      <el-form-item label="" prop="pwd" label-width="0">
        <el-input v-model="formData.password" placeholder="密码" clearable prefix-icon="el-icon-lock"
                  show-password class="form-item-input"></el-input>
      </el-form-item>
      <el-form-item label="" prop="repwd" label-width="0">
        <el-input v-model="formData.rePassword" placeholder="确认密码" clearable prefix-icon="el-icon-lock"
                  show-password class="form-item-input"></el-input>
      </el-form-item>
    </el-form>
    <el-button type="primary" class="form-item-input" @click="handleReset">提交修改</el-button>
    <div class="flex-center bottom-help">
      <el-link type="primary" @click="go('/')">返回首页</el-link>
      <el-divider direction="vertical"></el-divider>
      <el-link type="primary" @click="go('/dashboard')">返回工作台</el-link>
    </div>
  </el-card>
</template>

<script>
import Logo from '@/components/Logo'
export default {
  name: 'ResetPasswordForm',
  components: {
    Logo
  },
  data () {
    return {
      formData: {
        password: undefined,
        rePassword: undefined
      },
      rules: {
        password: [
          this.$validate.necessary('请输入密码'),
          this.$validate.length(6, 16, '密码')
        ],
        rePassword: [
          this.$validate.necessary('请再次输入密码'),
          this.$validate.length(6, 16, '确认密码')
        ]
      }
    }
  },
  methods: {
    handleReset () {
      this.$refs['resetForm'].validate(valid => {
        if (!valid) {
          this.$notify.error('表单验证失败')
          return
        }
        if (!(this.formData.password === this.formData.rePassword)) {
          this.$notify.error('两次输入的密码不一致')
          return
        }
        this.$store.dispatch('ResetPassword', this.formData).then(() => {
          this.$notify.success('密码修改成功，请重新登录' + this.$store.getters.token)
          this.$router.push('/login')
        }).catch(res => {
          this.$notify.error({
            title: '密码修改失败',
            message: res.msg
          })
        })
      })
    },
    go (url) {
      this.$router.push(url)
    }
  }
}
</script>

<style lang="scss" scoped>
.flex-center {
  display: flex;
  justify-content: center;
}
.form-item-input {
  width: 100%;
}
.login-code {
  /* 改变鼠标移动到验证码容器上时的样式 */
  cursor: pointer;
}
.bottom-help {
  margin-top: 20px;
}
</style>
